﻿/*!
 * @Description:通用数据绑定js,数据源是sysparm.js，这个js是针对前面js的各种应用
 * @Author:zb,yangpl
 * @Date:2014-07-22
*/
var dataBind = {};

//根据数据类型获取需要的真是数据源(type:SysParam的ParameterType值)
dataBind.getRealDataByType = function (type) {
    var tempItems = [];
    for (var i = 0; i < SysParam.length; i++) {
        if (SysParam[i].ParameterType == type) {
            for (var j = 0; j < SysParam[i].Items.length; j++) {
                if (SysParam[i].Items[j].ItemStatus == '1') {
                    tempItems.push(SysParam[i].Items[j]);
                }
            }

        }
    }

    var compareSeq = function (obj1, obj2) {
        var val1 = obj1.Seq;
        var val2 = obj2.Seq;
        if (val1 < val2) {
            return -1;
        } else if (val1 > val2) {
            return 1;
        } else {
            return 0;
        }
    };
    tempItems = tempItems.sort(compareSeq);
    return tempItems;
};

//根据数据类型获取需要的子类ID
dataBind.getKeyIdsByType = function (type) {
    var tempItems = [];
    for (var i = 0; i < SysParam.length; i++) {
        if (SysParam[i].ParameterType == type) {
            for (var j = 0; j < SysParam[i].Items.length; j++) {
                if (SysParam[i].Items[j].ItemStatus == '1') {
                    tempItems.push(SysParam[i].Items[j].ItemValue);
                }
            }
        }
    }
    return tempItems.join();
};
//根据类型,code获得id
dataBind.getKeyIdByTypeAndCode = function (type, code) {
    var tempItems = [];
    for (var i = 0; i < SysParam.length; i++) {
        if (SysParam[i].ParameterType == type) {
            for (var j = 0; j < SysParam[i].Items.length; j++) {
                if (SysParam[i].Items[j].ItemStatus == '1' && SysParam[i].Items[j].ItemCode == code) {
                    return SysParam[i].Items[j].ItemValue;
                }
            }
        }
    }
    return "";
};
//根据类型,keyId获得value
dataBind.getValueByTypeAndKeyId = function (type, keyId) {
    var tempItems = [];
    for (var i = 0; i < SysParam.length; i++) {
        if (SysParam[i].ParameterType == type) {
            for (var j = 0; j < SysParam[i].Items.length; j++) {
                if (SysParam[i].Items[j].ItemStatus == '1' && SysParam[i].Items[j].ItemValue == keyId) {
                    return SysParam[i].Items[j].ItemText;
                }
            }
        }
    }
    return "";
};

//根据数据类型生成下拉框的Option追加到选择器元素中(type：SysParam的ParameterType值，selector：要追加的选择器，isItemCode：生成的option的Val是否绑定SysParam的ItemCode。默认绑定SysParam的ItemValue[类型是GUID])
dataBind.bindOption = function (type, selector, isItemCode) {
    var data = dataBind.getRealDataByType(type);
    if (isItemCode) {
        $(selector).each(function () {
            for (var i = 0; i < data.length; i++) {
                var selected = "";
                if (data[i].ItemValue == $(this).attr("bindvalue")) {
                    selected = 'selected="selected"';
                }
                $(this).append('<option value="' + data[i].ItemCode + '"  ' + selected + '   >' + data[i].ItemText + '</option>');
            }
        });
    } else {
        $(selector).each(function () {
            for (var i = 0; i < data.length; i++) {
                var selected = "";
                if (data[i].ItemValue == $(this).attr("bindvalue")) {
                    selected = "selected";
                }
                $(this).append("<option value='" + data[i].ItemValue + "' " + selected + "  >" + data[i].ItemText + "</option>");
            }
        });
    }
};

//根据数据类型生成下拉框的Option追加到选择器元素中(type：SysParam的ParameterType值，selector：要追加的选择器，isItemCode：生成的option的Val是否绑定SysParam的ItemCode。默认绑定SysParam的ItemValue[类型是GUID])
dataBind.bindOptionByKeyId = function (type, selector, isItemCode) {
    var data = dataBind.getRealDataByType(type);
    if (isItemCode) {
        $(selector).each(function () {
            for (var i = 0; i < data.length; i++) {
                var selected = "";
                if (data[i].ItemValue == isItemCode) {
                    selected = 'selected="selected"';
                }
                $(this).append('<option value="' + data[i].ItemValue + '"  ' + selected + '   >' + data[i].ItemText + '</option>');
            }
        });
    } else {
        $(selector).each(function () {
            for (var i = 0; i < data.length; i++) {
                var selected = "";
                if (data[i].ItemValue == $(this).attr("bindvalue")) {
                    selected = "selected";
                }
                $(this).append("<option value='" + data[i].ItemValue + "' " + selected + "  >" + data[i].ItemText + "</option>");
            }
        });
    }
};

//根据数据类型生成radio追加到选择器元素中(type：SysParam的ParameterType值，selector：要追加的选择器，name：radio分组名，filterCondition：过滤条件[不需要的项名称类型为数组])
dataBind.bindRadio = function (type, selector, name, filterCondition, iscode) {
    var data = dataBind.getRealDataByType(type);
    $(selector).each(function () {
        for (var i = 0; i < data.length; i++) {
            var checked = "";
            if (data[i].ItemValue == $(this).attr("bindvalue")) {
                checked = "checked";
            }
            var temstr = '';
            if (filterCondition) {
                var isContain = false;
                for (var j = 0; j < filterCondition.length; j++) {
                    if (filterCondition[j] == data[i].ItemText) {
                        isContain = true;
                        break;
                    }
                }
                if (isContain) {
                    continue;
                }
            }
            var temvalue = "";
            if (iscode) {
                temvalue = data[i].ItemCode;
            }
            else {
                temvalue = data[i].ItemValue;
            }
            if (data[i].ItemText != "中原成交")//深圳版新需求（排除中原成交）
            {
                temstr = "<label class='mr20 dib'><input type='radio' data-itemno= '" + data[i].ItemCode + "'" + "name='" + name + "' value='" + temvalue + "' " + checked + "  >" + data[i].ItemText + "</label>";
                $(this).append(temstr);
            }
            if (data[i].ItemText == "中原成交" && $(selector).attr("bindvalue") == data[i].ItemValue) {
                temstr = "<label class='mr20 dib'><input type='radio' data-itemno= '" + data[i].ItemCode + "'" + "name='" + name + "' value='" + temvalue + "' " + checked + "  >" + data[i].ItemText + "</label>";
                $(this).append(temstr);
            }


            
        }
    });
};

//根据数据类型生成<li>radio</li>追加到选择器元素中(type:绑定类型 selector:需要绑定的radio元素 name:radio分组名 btnId:button按钮Id filterCondition:过滤条件(不需要的项名称类型为数组))
dataBind.bindLiRadio = function (type, selector, name, btnId, filterCondition) {
    var data = dataBind.getRealDataByType(type);
    
    for (var i = 0; i < data.length; i++) {

        var checked = "";
        if (data[i].ItemValue == $(selector).attr("bindvalue")) {
            checked = "checked";
        }
        if (filterCondition) {
            var isContain = false;
            for (var j = 0; j < filterCondition.length; j++) {
                if (filterCondition[j] == data[i].ItemText) {
                    isContain = true;
                    break;
                }
            }
            if (isContain) {
                continue;
            }

        }
        if (data[i].ItemText != "中原成交")//深圳版新需求（排除中原成交）
        {
            var temstr = "<li><label><input type='radio' name='" + name + "' value='" + data[i].ItemValue + "' " + checked + "  >" + data[i].ItemText + "</label></li>";
            $(selector).append(temstr);
        }
        
    }

    $(selector).append('<li><input type="button" id="' + btnId + '" class="btn-red" value="保存" /></li>');
}

//通过type生成li的列表
dataBind.bindLiLink = function (type, selector, eventfunc, filterCondition) {
    var data = dataBind.getRealDataByType(type);
    var tempEvent = "";
    $(selector).each(function () {
        for (var i = 0; i < data.length; i++) {
            if (filterCondition) {
                var isContain = false;
                for (var j = 0; j < filterCondition.length; j++) {
                    if (filterCondition[j] == data[i].ItemText) {
                        isContain = true;
                        break;
                    }
                }
                if (isContain) {
                    continue;
                }

            }
            if (eventfunc) {
                tempEvent = 'onclick="' + eventfunc + '"';
            }
            var temstr = '<li><a  ' + tempEvent + '  itemvalue="' + data[i].ItemValue + '"myvalue="' + data[i].ExtendAttr + '">' + data[i].ItemText + '</a></li>';
            $(this).append(temstr);
        }
    });
};

//根据数据类型生成CheckBox追加到选择器元素中(type：SysParam的ParameterType值，selector：要追加的选择器，name：CheckBox分组名)
dataBind.bindCheckBox = function (type, selector, name) {
    var data = dataBind.getRealDataByType(type);
    $(selector).each(function () {
        for (var i = 0; i < data.length; i++) {
            var checked = "";
            //房源新增编辑时“委托房源”标签不显示
            if (data[i].ItemText == '委托房源') {
                continue;
            }
            if ($(this).attr("bindvalue")) {
                var temarray = $(this).attr("bindvalue").split(",");
                for (var j = 0; j < temarray.length; j++) {
                    if (data[i].ItemValue == temarray[j]) {
                        checked = "checked";
                    }
                }
            }
            var temstr = "<label class='mr20 dib'><input type='checkbox' name='" + name + "' value='" + data[i].ItemValue + "' " + checked + "  >" + data[i].ItemText + "</label>";
            $(this).append(temstr);
        }
    });
}

//房源跟进类型按条件绑定显示radio按钮
dataBind.bindRadioByPropertyCondition = function (type, selector, name, extendAttr, authorityParameter) {
    var data = [], tempAuthorityParameter = [];
    if (authorityParameter) {
        tempAuthorityParameter = authorityParameter.split(",");
    }
    for (var i = 0; i < SysParam.length; i++) {
        if (SysParam[i].ParameterType == type) {
            for (var j = 0; j < SysParam[i].Items.length; j++) {
                if (SysParam[i].Items[j].ItemStatus == "1" && SysParam[i].Items[j].ExtendAttr == extendAttr) {
                    if (SysParam[i].Items[j].FlagDefault == true) {
                        if (dataBind.checkAuthority(tempAuthorityParameter, SysParam[i].Items[j].ItemCode)) {
                            data.push(SysParam[i].Items[j]);
                        }
                    } else {
                        data.push(SysParam[i].Items[j]);
                    }
                }
            }
        }
    }
    var tempStr = '<span class="l w580">';
    for (var k = 0; k < data.length; k++) {
        //过滤特殊跟进类型项，带看 1120,行家动态 1180，实勘 1140,实看1150，签约条件1090
        if (data[k].ItemCode != "1120" && data[k].ItemCode != "1180" && data[k].ItemCode != "1140"
            && data[k].ItemCode != "1150" && data[k].ItemCode != "1090") {
            tempStr += '<label class="mr20 dib"><input type="radio" name="' + name + '" value="' + data[k].ItemValue + ',' + data[k].ItemCode + ',' + data[k].ItemText + '" itemcode="' + data[k].ItemCode + '" >' + data[k].ItemText + '</label>';
        }
    }
    tempStr += '</span>';
    $(selector).append(tempStr);
};


//房源跟进类型radio按钮按条件绑定显示
dataBind.bindRadioFollowType = function (type, selector, name, extendAttr, authorityParameter, propertystatus,trustType) {
    var data = [], tempAuthorityParameter = [];
    if (authorityParameter) {
        tempAuthorityParameter = authorityParameter.split(",");
    }
    for (var i = 0; i < SysParam.length; i++) {
        if (SysParam[i].ParameterType == type) {
            for (var j = 0; j < SysParam[i].Items.length; j++) {
                if (SysParam[i].Items[j].ItemStatus == "1" && SysParam[i].Items[j].ExtendAttr == extendAttr) {
                    if (SysParam[i].Items[j].FlagDefault == true) {
                        if (dataBind.checkAuthority(tempAuthorityParameter, SysParam[i].Items[j].ItemCode)) {
                            data.push(SysParam[i].Items[j]);
                        }
                    } else {
                        data.push(SysParam[i].Items[j]);
                    }
                }
            }
        }
    }
    var tempStr = '<span class="l w580">';
    for (var k = 0; k < data.length; k++) {
        if (propertystatus.split('=')[1] == "有效") {
            if (trustType == "BOTH") {
                //过滤新开盘 2110
                if (data[k].ItemCode != "2110" && data[k].ItemCode != "10") {
                    tempStr += '<label class="mr20 dib"><input type="radio" name="' + name + '" value="' + data[k].ItemValue + ',' + data[k].ItemCode + ',' + data[k].ItemText + '" itemcode="' + data[k].ItemCode + '" >' + data[k].ItemText + '</label>';
                }
            } else {
                //过滤洗盘 10
                if (data[k].ItemCode != "10") {
                    tempStr += '<label class="mr20 dib"><input type="radio" name="' + name + '" value="' + data[k].ItemValue + ',' + data[k].ItemCode + ',' + data[k].ItemText + '" itemcode="' + data[k].ItemCode + '" >' + data[k].ItemText + '</label>';
                }
            }
        }
        else if (propertystatus.split('=')[1] == "预定") {
            //过滤洗盘 10
            if (data[k].ItemCode != "10" && data[k].ItemCode != "2110") {
                tempStr += '<label class="mr20 dib"><input type="radio" name="' + name + '" value="' + data[k].ItemValue + ',' + data[k].ItemCode + ',' + data[k].ItemText + '" itemcode="' + data[k].ItemCode + '" >' + data[k].ItemText + '</label>';
            }
        }
        else {
            //过滤补充信息
            if (data[k].ItemCode != "2030") {
                tempStr += '<label class="mr20 dib"><input type="radio" name="' + name + '" value="' + data[k].ItemValue + ',' + data[k].ItemCode + ',' + data[k].ItemText + '" itemcode="' + data[k].ItemCode + '" >' + data[k].ItemText + '</label>';
            }
        }
    }
    tempStr += '</span>';
    $(selector).append(tempStr);
};


//房源跟进类型权限检查
dataBind.checkAuthority = function (authorityParameter, itemCode) {
    if (!authorityParameter) {
        return false;
    }
    if (itemCode == "10") {
        return dataBind.checkAuthorityDetails(authorityParameter, "Property.FollowClean.Add");
    }
    if (itemCode == "2110") {
        return dataBind.checkAuthorityDetails(authorityParameter, "Property.FollowNewOpening.Add");
    }
    if (itemCode == "2000") {
        return dataBind.checkAuthorityDetails(authorityParameter, "Property.FollowAgencyProcedures.Add");
    }
    if (itemCode == "20") {
        return dataBind.checkAuthorityDetails(authorityParameter, "Property.FollowNewContact.Add");
    }
    if (itemCode == "30") {
        return dataBind.checkAuthorityDetails(authorityParameter, "Property.FollowTurn.Add");
    }
    if (itemCode == "40") {
        return dataBind.checkAuthorityDetails(authorityParameter, "Property.FollowComment.Add");
    }
    if (itemCode == "50") {
        return dataBind.checkAuthorityDetails(authorityParameter, "Property.FollowSupervise.Add");
    }
    if (itemCode == "70") {
        return dataBind.checkAuthorityDetails(authorityParameter, "Property.TurnStatus.Other");
    }
    if (itemCode == "2030") {
        return dataBind.checkAuthorityDetails(authorityParameter, "Property.FollowInformation.Add");
    }
    if (itemCode == "1230") {
        return dataBind.checkAuthorityDetails(authorityParameter, "Property.FollowCallPrice.Add");
    }
    if (itemCode == "1160") {
        return dataBind.checkAuthorityDetails(authorityParameter, "Property.FollowTrust.Add");
    }
    return false;
};

//房源跟进类型权限检查详情
dataBind.checkAuthorityDetails = function (authorityParameter, authorityValue) {
    for (var i = 0; i < authorityParameter.length; i++) {
        if (authorityParameter[i] == authorityValue) {
            return true;
        }
    }
    return false;
};


//客源跟进类型按条件绑定显示radio按钮
dataBind.bindRadioByInquiryCondition = function (type, selector, name, extendAttr, privilegeJso, filterCondition) {
    var data = [];
    for (var i = 0; i < SysParam.length; i++) {
        if (SysParam[i].ParameterType == type) {
            for (var j = 0; j < SysParam[i].Items.length; j++) {
                if (SysParam[i].Items[j].ItemStatus == '1' && SysParam[i].Items[j].ExtendAttr == extendAttr) {
                    data.push(SysParam[i].Items[j]);
                }
            }
        }
    }

    $(selector).each(function () {
        var isBindvalue = $(this).attr("bindvalue");
        for (var i = 0; i < data.length; i++) {
            var checked = "";
            if (data[i].ItemValue == isBindvalue) {
                checked = "checked";
            }
            var temstr = '';
            if (filterCondition) {
                var isContain = false;
                for (var j = 0; j < filterCondition.length; j++) {
                    if (filterCondition[j] == data[i].ItemText) {
                        isContain = true;
                        break;
                    }
                }
                if (isContain) {
                    continue;
                }
            }
            if (data[i].ItemCode == '170') {
                if (privilegeJso['转客'] == 1) {
                    temstr = dataBind.bindRadioByInquiryConditionPart(i, isBindvalue, name, data[i], checked, extendAttr);
                }
            } else {
                temstr = dataBind.bindRadioByInquiryConditionPart(i, isBindvalue, name, data[i], checked, extendAttr);
            }

            $(this).append(temstr);
        }
    });
};

//客源跟进类型按条件绑定显示radio按钮的数据加载部分
dataBind.bindRadioByInquiryConditionPart = function (i, isBindvalue, name, data, checked, extendAttr) {
    var temstr = '';
    if (i == 0) {
        if (!isBindvalue) {
            temstr = '<label class="dib mr20"><input type="radio" name="' + name + '"  value="' + data.ItemValue + ',' + data.ItemCode + '"  itemcode="' + data.ItemCode + '" extendattr="' + extendAttr + '"  checked="checked">' + data.ItemText + '</label>';
        } else {
            temstr = '<label class="dib mr20"><input type="radio" name="' + name + '" value="' + data.ItemValue + ',' + data.ItemCode + '"  itemcode="' + data.ItemCode + '" extendattr="' + extendAttr + '" ' + checked + ' >' + data.ItemText + '</label>';
        }
    } else {
        temstr = '<label class="dib mr20"><input type="radio" name="' + name + '" value="' + data.ItemValue + ',' + data.ItemCode + '"  itemcode="' + data.ItemCode + '" extendattr="' + extendAttr + '" ' + checked + ' >' + data.ItemText + '</label>';
    }

    return temstr;
};